package com.heima.kafka.sample;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;

import java.util.Properties;

/**
 * @author JianKang
 * @create 2023-10-25 13:00
 */
public class ProducerQuickStart {
    public static void main(String[] args) {
        //1.kafka链接配置信息
        Properties properties = new Properties();
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "192.168.8.8:9092");
        //key和value的序列化
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer");
        //2.创建kafka生成者对象
        KafkaProducer<String, String> kafkaProducer = new KafkaProducer<>(properties);
        //3.发送消息
        for (int i = 0; i < 5; i++) {
            ProducerRecord<String, String> producerRecord = new ProducerRecord<>("itcast-topic-input", "hellow kafka");
            kafkaProducer.send(producerRecord);
        }

        //4.关闭消息通道
        kafkaProducer.close();
    }
}
